/**
 * Copyright (c) 2017-present, Liu Jinyong
 * All rights reserved.
 *
 * https://github.com/huanxsd/MeiTuan  
 * @flow
 */


import React, { PureComponent } from 'react'
import { StatusBar } from 'react-native'
import { StackNavigator, TabNavigator, TabBarBottom } from 'react-navigation'

import color from './widget/color'
import { screen, system } from './common'
import TabBarItem from './widget/TabBarItem'

import HomeScene from './scene/Home/HomeScene'
import ShopCartScene from './scene/ShopCart/ShopCartScene'
import FindScene from './scene/Find/FindScene'
import MineScene from './scene/Mine/MineScene'
import TestScene from './scene/Test/TestScene'
  
import ApplyForDzScene from './scene/Mine/ApplyForDz/ApplyForDzScene'

import ApplyForDzIng from './scene/Mine/ApplyForDz/ApplyForDzIng'
import WebScene from './widget/WebScene'
import GroupPurchaseScene from './scene/GroupPurchase/GroupPurchaseScene'
import RecommendMore from './scene/Home/tab/recommend/RecommendMore'
import SortScene from './scene/Sort/SortScene';
import SQLite from './common/DataBase'
import DetailScene_GroupPurchase from './scene/Home/tab/grouppurchase/DetailScene_GroupPurchase'

let sqlite = new SQLite();
const lightContentScenes = ['Home', 'Mine']

function getCurrentRouteName(navigationState: any) {
    if (!navigationState) {
        return null
    }
    const route = navigationState.routes[navigationState.index]
    // dive into nested navigators
    if (route.routes) {
        return getCurrentRouteName(route)
    }
    return route.routeName
}


class RootScene extends PureComponent<{}> {
    constructor() {
        super()

        //StatusBar.setTranslucent(true)
    }

    componentDidMount() {
        let db = sqlite.open();
        //建表
        sqlite.init();
       // sqlite.close();
    }
    render() {
        return (
            <Navigator />
        )
    }
}

const Tab = TabNavigator(
    {
        //主页
        Home: {
            screen: HomeScene,
            navigationOptions: ({ navigation }) => ({
                tabBarLabel: '首页',
                tabBarIcon: ({ focused, tintColor }) => (
                    <TabBarItem
                        tintColor={tintColor}
                        focused={focused}
                        normalImage={require('./img/tabbar/tabbar_homepage.png')}
                        selectedImage={require('./img/tabbar/tabbar_homepage_selected.png')}
                    />
                )
            }),
        },
         //分类
         Sort: {
            screen: SortScene,
            navigationOptions: ({ navigation }) => ({
                tabBarLabel: '分类',
                tabBarIcon: ({ focused, tintColor }) => (
                    <TabBarItem
                        tintColor={tintColor}
                        focused={focused}
                        normalImage={require('./img/tabbar/tabbar_sort.png')}
                        selectedImage={require('./img/tabbar/tabbar_sort_selected.png')}
                    />
                )
            }),
        },
        //发现页
        Find: {
            screen: FindScene,
            navigationOptions: ({ navigation }) => ({
                tabBarLabel: '发现',
                tabBarIcon: ({ focused, tintColor }) => (
                    <TabBarItem
                        tintColor={tintColor}
                        focused={focused}
                        normalImage={require('./img/tabbar/tabbar_find.png')}
                        selectedImage={require('./img/tabbar/tabbar_find_selected.png')}
                    />
                )
            }),
        },
        //购物车页
        Order: {
            screen: ShopCartScene,
            navigationOptions: ({ navigation }) => ({
                tabBarLabel: '购物车',
                tabBarIcon: ({ focused, tintColor }) => (
                    <TabBarItem
                        tintColor={tintColor}
                        focused={focused}
                        normalImage={require('./img/tabbar/tabbar_cart.png')}
                        selectedImage={require('./img/tabbar/tabbar_cart_selected.png')}
                    />
                )
            }),
        },
        //我的
        Mine: {
            screen: MineScene,
            navigationOptions: ({ navigation }) => ({
                tabBarLabel: '我的',
                tabBarIcon: ({ focused, tintColor }) => (
                    <TabBarItem
                        tintColor={tintColor}
                        focused={focused}
                        normalImage={require('./img/tabbar/tabbar_mine.png')}
                        selectedImage={require('./img/tabbar/tabbar_mine_selected.png')}
                    />
                )
            }),
        },
        //预留的测试页
        // Test: {
        //     screen: TestScene,
        //     navigationOptions: ({ navigation }) => ({
        //         tabBarLabel: 'Test',
        //         tabBarIcon: ({ focused, tintColor }) => (
        //             <TabBarItem
        //                 tintColor={tintColor}
        //                 focused={focused}
        //                 normalImage={require('./img/tabbar/tabbar_mine.png')}
        //                 selectedImage={require('./img/tabbar/tabbar_mine_selected.png')}
        //             />
        //         )
        //     }),
        // },
    },
    {
        tabBarComponent: TabBarBottom,
        tabBarPosition: 'bottom',
        lazy: true,
        animationEnabled: false,
        swipeEnabled: true,
        tabBarOptions: {
            activeTintColor:'#f25697',
            inactiveTintColor: '#979797',
            style: { backgroundColor: '#ffffff' },
        },
    }

)

const Navigator = StackNavigator(
    {
        Tab: { screen: Tab },
        Web: { screen: WebScene },
        GroupPurchase: { screen: GroupPurchaseScene },
        RecommendMore : {screen : RecommendMore},
        DetailScene_GroupPurchase: { screen : DetailScene_GroupPurchase},
        ApplyForDzScene:{screen:ApplyForDzScene},
        ApplyForDzIng:{screen:ApplyForDzIng},
    },
    {
        navigationOptions: {
            // headerStyle: { backgroundColor: color.primary }
            headerBackTitle: null,
            headerTintColor: '#333333',
            showIcon: true,
        },
    }
)

export default RootScene
